package logic

import (
	"context"
	"shop-manager/internal/repos/category"
	"shop-manager/internal/vo"
)

//go:generate newc
type CategoryLogic struct {
	categoryRepo *category.CategoryRepo
}

func (l *CategoryLogic) ListCategory(ctx context.Context, req vo.ListCategoryReq) (rsp *vo.ListCategoryRsp, err error) {
	listData, total, err := l.categoryRepo.ListCategory(ctx, category.ListCategoryParam{
		Page:          req.Page,
		PageSize:      req.PageSize,
		CategoryTypes: req.CategoryTypes,
		Search:        req.Search,
		Sorts:         req.Sorts,

		ReturnExtraFields: true,
	})
	if err != nil {
		return nil, err
	}
	rsp = &vo.ListCategoryRsp{
		Items: make([]*vo.Category, 0, len(listData)),
		Total: total,
	}

	for _, category := range listData {
		rsp.Items = append(rsp.Items, &vo.Category{
			ID:        category.ID,
			ClassID:   category.ClassID,
			LevelID:   category.LevelID,
			BrandID:   category.BrandID,
			CreatedAt: category.CreatedAt,
			UpdatedAt: category.UpdatedAt,

			ClassName: category.ClassName,
			LevelName: category.LevelName,
			BrandName: category.BrandName,
		})
	}

	return rsp, nil
}
